/**
 * Copyright 2018 人人开源 http://www.gbznt.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.gbznt.modules.sys.controller;

import io.gbznt.common.constant.StatusEnum;
import io.gbznt.common.utils.Constant;
import io.gbznt.common.utils.R;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.common.validator.ValidatorUtils;
import io.gbznt.common.validator.group.AddGroup;
import io.gbznt.common.validator.group.UpdateGroup;
import io.gbznt.modules.sys.constant.DeptTypeEnum;
import io.gbznt.modules.sys.entity.SysDeptEntity;
import io.gbznt.modules.sys.service.SysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门管理
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-20 15:23:47
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {
	@Autowired
	private SysDeptService sysDeptService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:dept:list")
	public List<SysDeptEntity> list(@RequestParam Map<String, Object> params) {
		List<SysDeptEntity> deptList = sysDeptService.queryList(params);
		return deptList;
	}


	/**
	 * 基地列表
	 */
	@RequestMapping("/areaList")
	public R areaList(@RequestParam Map<String, Object> params) {
		List<SysDeptEntity> deptList = sysDeptService.list(
				new QueryWrapper<SysDeptEntity>().eq("status",StatusEnum.NORMAL.getValue())
				.eq("type", DeptTypeEnum.AREA.getValue())
                .orderByAsc("order_num").orderByDesc("create_time")
		);

		return R.ok().put("list",deptList);
	}

	/**
	 * 选择部门(添加、修改菜单)
	 */
	@RequestMapping("/select")
	public R select() {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", StatusEnum.NORMAL.getValue() + "");
		List<SysDeptEntity> deptList = sysDeptService.queryList(params);

		// 添加一级部门
		if (getUserId().equals(Constant.SUPER_ADMIN)) {
			SysDeptEntity root = new SysDeptEntity();
			root.setId("0");
			root.setName("一级部门");
			root.setParentId("-1");
			root.setOpen(true);
			deptList.add(root);
		}

		return R.ok().put("deptList", deptList);
	}

	/**
	 * 选择部门(添加、修改菜单)
	 */
	@RequestMapping("/select2")
	public R select2() {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", StatusEnum.NORMAL.getValue() + "");
		List<SysDeptEntity> deptList = sysDeptService.queryList(params);

		return R.ok().put("deptList", deptList);
	}
	
	/**
	 * 选择部门(添加、修改菜单)
	 */
	@RequestMapping("/select3")
	public R select3(@RequestParam Map<String, Object> params) {
		//Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", StatusEnum.NORMAL.getValue() + "");
		List<SysDeptEntity> deptList = sysDeptService.queryList(params);

		return R.ok().put("deptList", deptList);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{deptId}")
	@RequiresPermissions("sys:dept:info")
	public R info(@PathVariable("deptId") String deptId) {
		SysDeptEntity dept = sysDeptService.getById(deptId);

		return R.ok().put("dept", dept);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:dept:save")
	public R save(@RequestBody SysDeptEntity dept) {
		ValidatorUtils.validateEntity(dept, AddGroup.class);

		dept.setCreateId(ShiroUtils.getUserId());
		dept.setCreateName(ShiroUtils.getUserName());
		dept.setCreateTime(new Date());
		sysDeptService.save(dept);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:dept:update")
	public R update(@RequestBody SysDeptEntity dept) {
		ValidatorUtils.validateEntity(dept, UpdateGroup.class);

		dept.setUpdateId(ShiroUtils.getUserId());
		dept.setUpdateName(ShiroUtils.getUserName());
		dept.setUpdateTime(new Date());
		sysDeptService.updateById(dept);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:dept:delete")
	public R delete(@RequestBody String[] ids) {
		QueryWrapper<SysDeptEntity> qw = new QueryWrapper<SysDeptEntity>();
		qw.eq("parent_id", ids[0]);
		List<SysDeptEntity> list = sysDeptService.list(qw);
		int a = 0;
		if (list.size() < 1) {
			sysDeptService.removeById(ids[0]);
		} else {
			a = 1;
		}
		return R.ok().put("a", a);
	}

}
